﻿using System;
using System.Web;
using System.Web.Mvc;
using Willow.Common;

namespace Willow.MVC.Result
{
    /// <summary>
    /// 标准JSON结果集
    /// </summary>
    public class StandardJsonResult : JsonResult
    {
        /// <summary>
        /// 结果
        /// </summary>
        public bool Result { get; set; }
        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="message">The message.</param>
        private void Succeed(string message)
        {
            this.Result = true;
            this.Message = message;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message">The message.</param>
        private void Fail(string message)
        {
            this.Result = false;
            this.Message = message;
        }

        /// <summary>
        /// Tries the specified action.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="succeedMsg">The succeed MSG.</param>
        public void Try(Action action, string succeedMsg)
        {
            try
            {
                action();
                this.Succeed(succeedMsg);
            }
            catch (WarningException ex)
            {
                this.Fail(ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public StandardJsonResult()
        {
            this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var isHttpGet = context.HttpContext.Request.HttpMethod.Equals("get");
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && isHttpGet)
            {
                const string error = @"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
                throw new InvalidOperationException(error);
            }

            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            response.Write(js.Serialize(this.ToCustomResult()));
        }

        /// <summary>
        /// 自定义返回结果
        /// </summary>
        /// <returns></returns>
        protected virtual object ToCustomResult()
        {
            var result = new
            {
                this.Result,
                this.Message
            };
            return result;
        }

    }
}
